<?php

function resize_jpg( $imgage_file_path, $new_image_file_path, $width, $height)
{
	//resize theo width thi $height=0; resize theo height thi $width=0; binh thuong se resize theo width
    if(function_exists("gd_info"))
    {
		$size=@getimagesize($imgage_file_path);
		if($size[0]<=$width)
		{
			$width = $size[0];			
		}
		
		if($size[1]<=$heigh)
		{
			$height = $size[1];			
		}
		
		if($width==0 && $height!=0)
			$width=ceil($size[0]/($size[1]/$height));
		elseif($width!=0 && $height==0)
			$height=ceil($size[1]/($size[0]/$width));
		elseif($width!=0 && $height!=0)
		{
		    $heightrel=ceil($size[1]/($size[0]/$width));
			if($heightrel>$height)
			{			   
			   $width=ceil($size[0]/($size[1]/$height));
			}
			else 
			{
			   $height = $heightrel;
			}
		}	
								
		switch ($size[2]) {
			case 1:
				$img = @imagecreatefromgif($imgage_file_path);
				break;
			case 2:
				$img = @imagecreatefromjpeg($imgage_file_path);
				break;
			case 3:
				$img = @imagecreatefrompng($imgage_file_path);
				break;
		}	
						
        $thumb=@imagecreatetruecolor($width,$height);
        @imagecopyresampled($thumb,$img,0,0,0,0,imagesx($thumb),imagesy($thumb),imagesx($img),imagesy($img));
		switch ($size[2]) {
			case 1:
				@imagegif($thumb, $new_image_file_path) or die('Permanent batch resizing failed. Please enable write access to the files in this directory (chmod 0777 directory/ -R)');
				break;
			case 2:
				@imagejpeg( $thumb, $new_image_file_path, 80) or die('Permanent batch resizing failed. Please enable write access to the files in this directory (chmod 0777 directory/ -R)');
				break;
			case 3:
				@imagepng($thumb, $new_image_file_path) or die('Permanent batch resizing failed. Please enable write access to the files in this directory (chmod 0777 directory/ -R)');
				break;
		}
        //@touch($new_image_file_path,$date);
        @imagedestroy($img);
        @imagedestroy($thumb);
    }

}



?>
